﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PIF1006_TP1
{
    public partial class FrmPartie1 : Form
    {
        public FrmPartie1()
        {
            InitializeComponent();
        }

        private void enabledParameter(bool state)
        {
            txtParametre.Enabled = state;
            pTriangleParam.Visible = false;
        }

        private void rbAddition_CheckedChanged(object sender, EventArgs e)
        {
            enabledParameter(true);
        }

        private void rbMultiplication_CheckedChanged(object sender, EventArgs e)
        {
            enabledParameter(true);
        }

        private void rbTrace_CheckedChanged(object sender, EventArgs e)
        {
            enabledParameter(false);
        }

        private void rbDeterminant_CheckedChanged(object sender, EventArgs e)
        {
            enabledParameter(false);
        }

        private void rbTranspose_CheckedChanged(object sender, EventArgs e)
        {
            enabledParameter(false);
        }

        private void rbComatrice_CheckedChanged(object sender, EventArgs e)
        {
            enabledParameter(false);
        }

        private void rbInverse_CheckedChanged(object sender, EventArgs e)
        {
            enabledParameter(false);
        }

        private void rbCarre_CheckedChanged(object sender, EventArgs e)
        {
            enabledParameter(false);
        }

        private void rbTriangle_CheckedChanged(object sender, EventArgs e)
        {
            enabledParameter(true);
            txtParametre.Clear();
            pTriangleParam.Visible = true;
        }

        private void rbRegular_CheckedChanged(object sender, EventArgs e)
        {
            enabledParameter(false);
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            txtResultat.Clear();

            try
            {

                Matrix matrix = Matrix.CreateMatrixFromString(txtMatrice.Text);

                if (rbAddition.Checked == true)
                {
                    Matrix addMatrix = Matrix.CreateMatrixFromString(txtParametre.Text);
                    txtResultat.Text = matrix.AddMatrice(addMatrix).ToString();
                }
                else if (rbMultiplication.Checked == true)
                {
                    int scalar;

                    //vérification si le parametre est un nombre
                    if (int.TryParse(txtParametre.Text, out scalar))
                    {
                        txtResultat.Text = matrix.ScalarProduct(scalar).ToString();
                    }
                    else
                    {
                        Matrix productMatrix = Matrix.CreateMatrixFromString(txtParametre.Text);
                        txtResultat.Text = matrix.MatrixProduct(productMatrix).ToString();
                    }
                }
                else if (rbTrace.Checked == true)
                {
                    txtResultat.Text = matrix.Trace.ToString();
                }
                else if (rbDeterminant.Checked == true)
                {
                    txtResultat.Text = matrix.Determinant().ToString();
                }
                else if (rbTranspose.Checked == true)
                {
                    txtResultat.Text = matrix.TransposedMatrix.ToString();
                }
                else if (rbComatrice.Checked == true)
                {
                    txtResultat.Text = matrix.AdjugateMatrix.ToString();
                }
                else if (rbInverse.Checked == true)
                {
                    txtResultat.Text = matrix.InverseMatrix.ToString();
                }
                else if (rbCarre.Checked == true)
                {
                    txtResultat.Text = matrix.IsSquare.ToString();
                }
                else if (rbTriangle.Checked == true)
                {
                    //vérification des paramètres
                    if (txtParametre.TextLength >= 2)
                    {
                        string[] parameters = txtParametre.Text.Split(',');
                        bool strict = parameters[1] == "1";
                        TriangularType type = (TriangularType)(Convert.ToInt32(parameters[0]));

                        txtResultat.Text = matrix.GetIsTriangular(type, strict).ToString();
                    }
                    else
                        throw new Exception("Paramètres manquants");
                }
                else if (rbRegular.Checked == true)
                {
                    txtResultat.Text = matrix.IsRegular.ToString();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
    }
}
